package thread.wang_bao_ling.c08;

import java.util.concurrent.CountDownLatch;
import java.util.stream.IntStream;

public class Test3 {
    private static int count=0;
    private static String name= String.valueOf("luobiao");
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(100);

        IntStream.range(0, 100).forEach(f->{
            new Thread(()->{
                for (int i = 0; i < 1000; i++) {
                    synchronized (name) {
                        System.out.println(Thread.currentThread().getName()+"|count="+count);
                        count++;
                    }
                }
                countDownLatch.countDown();
            },"thread-"+f).start();
        });


        new Thread(()->{

            //由于valueOf会重用常量池中的"luobiao"，所以此处对字面量"luobiao"加锁会阻塞上面的线程
            synchronized ("luobiao") {
                System.out.println("blocking get lock");
                try {
                    //占用锁，会导致count++无法进行
                    Thread.sleep(3000);
                    System.out.println("end sleep");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        countDownLatch.await();
        System.out.println(count);

    }
}
